package thoven.achievement.net.ideapluginachievement.model

import com.intellij.util.xmlb.annotations.OptionTag
import thoven.achievement.net.ideapluginachievement.util.LocalDateConverter
import java.time.LocalDate
import java.io.Serializable

/**
 * 代码统计数据模型(测试推送)
 */
data class CodeStats(
    @OptionTag(converter = LocalDateConverter::class)
    var date: LocalDate = LocalDate.now(),
    var addedLines: Int = 0,
    var deletedLines: Int = 0,
    var language: String = "Unknown"
) : Serializable {
    val netLines: Int
        get() = addedLines - deletedLines
    
    // 无参构造函数，用于序列化
    constructor() : this(LocalDate.now(), 0, 0, "Unknown")
}

/**
 * 累计的代码统计
 */
data class AggregatedCodeStats(
    val todayAdded: Int = 0,
    val todayDeleted: Int = 0,
    val totalAdded: Int = 0,
    val totalDeleted: Int = 0,
    val totalNet: Int = 0
) : Serializable

/**
 * 成就进度信息
 */
data class AchievementProgress(
    val type: AchievementType,
    val name: String,
    val description: String,
    val icon: String,
    val currentValue: Int,
    val targetValue: Int,
    val progressPercent: Int,
    val isUnlocked: Boolean
) : Serializable {
    /**
     * 是否接近完成（进度 >= 80%）
     */
    fun isCloseToUnlock(): Boolean = progressPercent >= 80 && !isUnlocked
    
    /**
     * 距离目标还差多少
     */
    fun remainingValue(): Int = (targetValue - currentValue).coerceAtLeast(0)
}

/**
 * 成就类型
 */
enum class AchievementType {
    // 按行数
    WARM_UP,            // 热身完毕: 一天写满100行
    KEYBOARD_FLYING,    // 键盘起飞: 一天写满300行
    HARD_WORKER,        // 爆肝工程师: 一天写满500行
    LEGENDARY_1000,     // 传说的一千行: 一天写满1000行
    CODE_MACHINE,       // 代码生产机器: 累计写满10,000行
    
    // 按删除
    DELETE_DB,          // 果断删库: 一天删除200行
    REFACTOR_MASTER,    // 重构大师: 累计删除5000行
    DELETE_AND_RUN,     // 删库跑路传说: 一次删除1000行以上
    
    // 按时间
    EARLY_BIRD,         // 早起的程序猿: 早上9点前写下第一行代码
    NIGHT_CODER,        // 深夜键仙: 凌晨1点后还在写代码
    MARATHON_CODER,     // 马拉松选手: 连续8小时写代码
    WEEKEND_WARRIOR,    // 周末爆肝王: 周末写满300行
    
    // 按连续性
    SIGN_IN_DAY1,       // 签到第一天: 连续写代码1天
    CRAZY_7_DAYS,       // 七日狂热者: 连续写代码7天
    IRON_30_DAYS,       // 铁人三十天: 连续写代码30天
    DREAM_100_DAYS,     // 百日筑梦师: 连续写代码100天
    
    // 趣味彩蛋
    BUG_HARVESTER,      // Bug收割机: 一天内写下TODO/FIXME注释≥5次
    CTRL_S_BELIEVER,    // Ctrl+S信徒: 一天保存≥200次
    COPY_PASTE_MASTER,  // Copy-Paste大师: 一天粘贴代码≥100次
    IDEA_RESTARTER,     // IDEA重启侠: 一天重启IDEA≥5次
    CONSOLE_LOG_PRIEST, // console.log教徒: 打印日志≥50次
    
    // 网络热梗成就
    CHICKEN_YOU_ARE_TOO_BEAUTIFUL, // 鸡你太美: 频繁切换窗口编写代码
    HAVE_INSIDER_ABORT_DEAL,       // 有内鬼，终止交易: 发现严重bug后紧急回滚
    ABSOLUTELY_AMAZING,            // 绝绝子: 一天内解锁3个以上成就
    NOT_UNUSABLE,                  // 又不是不能用: 写出勉强能跑的代码
    TERRIFYING_WHEN_THINK,         // 细思极恐: 写出复杂度过高的代码
    PARALYZED,                     // 麻了: 连续遇到5个以上编译错误
    CANT_HOLD_IT,                  // 蚌埠住了: 写出让人无语的代码
    PURE_GRUDGE_HOLDER,           // 纯纯大怨种: 深夜加班写代码到凌晨3点
    DEFENSE_BROKEN,               // 破防了: 自己的代码被别人大量重构
    ART_IS_EXPLOSION,             // 艺术就是爆炸: 一次性删除500行以上代码
    YYDS_FOREVER,                 // YYDS永远的神: 连续30天每天都有代码提交
    WHAT_HAPPENED,                // 出什么事了: 紧急修复线上bug
    SOCIAL_DEATH,                 // 社死: 在代码注释中写了奇怪的内容
    LYING_FLAT,                   // 躺平: 一周内代码量少于10行
    INVOLUTION_KING               // 内卷之王: 周末写代码超过工作日
}

/**
 * 成就数据模型
 */
data class Achievement(
    var type: AchievementType = AchievementType.WARM_UP,
    
    @OptionTag(converter = LocalDateConverter::class)
    var date: LocalDate = LocalDate.now(),
    
    var unlocked: Boolean = false,
    
    // 新增：解锁时间戳（精确到毫秒），用于判断是否显示NEW标签
    // 向后兼容：默认为0，旧数据会自动使用默认值
    var unlockTimestamp: Long = 0L
) : Serializable {
    // 无参构造函数，用于序列化
    constructor() : this(AchievementType.WARM_UP, LocalDate.now(), false, 0L)
    
    /**
     * 判断是否是新解锁的成就（24小时内）
     */
    fun isNew(): Boolean {
        if (unlockTimestamp == 0L) return false
        val now = System.currentTimeMillis()
        val twentyFourHours = 24 * 60 * 60 * 1000L
        return (now - unlockTimestamp) < twentyFourHours
    }
    
    /**
     * 获取成就名称
     */
    fun getName(): String {
        return when (type) {
            // 按行数
            AchievementType.WARM_UP -> "热身完毕"
            AchievementType.KEYBOARD_FLYING -> "键盘起飞"
            AchievementType.HARD_WORKER -> "爆肝工程师"
            AchievementType.LEGENDARY_1000 -> "传说的一千行"
            AchievementType.CODE_MACHINE -> "代码生产机器"
            
            // 按删除
            AchievementType.DELETE_DB -> "果断删库"
            AchievementType.REFACTOR_MASTER -> "重构大师"
            AchievementType.DELETE_AND_RUN -> "删库跑路传说"
            
            // 按时间
            AchievementType.EARLY_BIRD -> "早起的程序猿"
            AchievementType.NIGHT_CODER -> "深夜键仙"
            AchievementType.MARATHON_CODER -> "马拉松选手"
            AchievementType.WEEKEND_WARRIOR -> "周末爆肝王"
            
            // 按连续性
            AchievementType.SIGN_IN_DAY1 -> "连续编码第一天"
            AchievementType.CRAZY_7_DAYS -> "一周坚持"
            AchievementType.IRON_30_DAYS -> "月度坚持"
            AchievementType.DREAM_100_DAYS -> "百日筑基"
            
            // 趣味彩蛋
            AchievementType.BUG_HARVESTER -> "Bug收割机"
            AchievementType.CTRL_S_BELIEVER -> "Ctrl+S信徒"
            AchievementType.COPY_PASTE_MASTER -> "Copy-Paste大师"
            AchievementType.IDEA_RESTARTER -> "IDEA重启侠"
            AchievementType.CONSOLE_LOG_PRIEST -> "console.log教徒"
            
            // 网络热梗成就
            AchievementType.CHICKEN_YOU_ARE_TOO_BEAUTIFUL -> "鸡你太美"
            AchievementType.HAVE_INSIDER_ABORT_DEAL -> "有内鬼，终止交易"
            AchievementType.ABSOLUTELY_AMAZING -> "绝绝子"
            AchievementType.NOT_UNUSABLE -> "又不是不能用"
            AchievementType.TERRIFYING_WHEN_THINK -> "细思极恐"
            AchievementType.PARALYZED -> "麻了"
            AchievementType.CANT_HOLD_IT -> "蚌埠住了"
            AchievementType.PURE_GRUDGE_HOLDER -> "纯纯大怨种"
            AchievementType.DEFENSE_BROKEN -> "破防了"
            AchievementType.ART_IS_EXPLOSION -> "艺术就是爆炸"
            AchievementType.YYDS_FOREVER -> "YYDS永远的神"
            AchievementType.WHAT_HAPPENED -> "出什么事了"
            AchievementType.SOCIAL_DEATH -> "社死"
            AchievementType.LYING_FLAT -> "躺平"
            AchievementType.INVOLUTION_KING -> "内卷之王"
        }
    }
    
    /**
     * 获取成就描述
     */
    fun getDescription(): String {
        return when (type) {
            // 按行数
            AchievementType.WARM_UP -> "写得不多，但IDE已经开始卡了 🙃"
            AchievementType.KEYBOARD_FLYING -> "代码不多，Bug已经先跑了 🏃💨"
            AchievementType.HARD_WORKER -> "摸鱼失败，爆肝成功，老板笑嘻嘻 😏"
            AchievementType.LEGENDARY_1000 -> "隔壁产品经理以为你在写小说 📖"
            AchievementType.CODE_MACHINE -> "键盘申请带薪休假中…… ⌨️💔"
            
            // 按删除
            AchievementType.DELETE_DB -> "删掉的不是代码，是青春的眼泪 😭"
            AchievementType.REFACTOR_MASTER -> "今天写的，明天删掉，后天继续写 ♻️"
            AchievementType.DELETE_AND_RUN -> "你确定不是rm -rf /？ ⚠️"
            
            // 按时间
            AchievementType.EARLY_BIRD -> "你写代码的样子，比早八的学生还拼 🌞"
            AchievementType.NIGHT_CODER -> "不修Bug，修仙呢？ 👻"
            AchievementType.MARATHON_CODER -> "坐着坐着，你悟道了：Bug生于代码 🧘"
            AchievementType.WEEKEND_WARRIOR -> "别人有生活，你有deadline 📅"
            
            // 按连续性
            AchievementType.SIGN_IN_DAY1 -> "好耶！今天也在为KPI努力搬砖 🧱"
            AchievementType.CRAZY_7_DAYS -> "别的UP主七天涨粉，你七天涨Bug 🐛"
            AchievementType.IRON_30_DAYS -> "你比健身房的私教还自律 💪"
            AchievementType.DREAM_100_DAYS -> "别人百日誓师，你百日写屎山 🗿"
            
            // 趣味彩蛋
            AchievementType.BUG_HARVESTER -> "未来的自己看到会原地爆炸 💣"
            AchievementType.CTRL_S_BELIEVER -> "保存是信仰，撤销是宿命 🙏"
            AchievementType.COPY_PASTE_MASTER -> "Stack Overflow：你最忠实的IDE 🦾"
            AchievementType.IDEA_RESTARTER -> "重启大法好，一键驱鬼术 👹"
            AchievementType.CONSOLE_LOG_PRIEST -> "Debug不求人，全靠log 📜"
            
            // 网络热梗成就
            AchievementType.CHICKEN_YOU_ARE_TOO_BEAUTIFUL -> "蔡徐坤看了都说6 🏀"
            AchievementType.HAVE_INSIDER_ABORT_DEAL -> "代码里有叛徒，快跑！ 🏃‍♂️"
            AchievementType.ABSOLUTELY_AMAZING -> "今天的你就是绝世好宝贝 ✨"
            AchievementType.NOT_UNUSABLE -> "虽然丑但是能跑就行了 🤷‍♂️"
            AchievementType.TERRIFYING_WHEN_THINK -> "这代码想想都觉得可怕 😱"
            AchievementType.PARALYZED -> "已经不知道该怎么修bug了 😵"
            AchievementType.CANT_HOLD_IT -> "看到这代码我蚌埠住了 🤣"
            AchievementType.PURE_GRUDGE_HOLDER -> "深夜敲代码的怨种就是你 😭"
            AchievementType.DEFENSE_BROKEN -> "代码被改得面目全非，破防了 💔"
            AchievementType.ART_IS_EXPLOSION -> "迪达拉看了都点赞 💥"
            AchievementType.YYDS_FOREVER -> "你就是代码界的永远滴神 👑"
            AchievementType.WHAT_HAPPENED -> "凌晨3点被叫起来修bug 📞"
            AchievementType.SOCIAL_DEATH -> "代码注释写得太羞耻了 😳"
            AchievementType.LYING_FLAT -> "这就是传说中的躺平青年 🛏️"
            AchievementType.INVOLUTION_KING -> "周末还在卷，你就是内卷之王 👑"
        }
    }
    
    /**
     * 获取成就图标
     */
    fun getIcon(): String {
        return when (type) {
            // 按行数
            AchievementType.WARM_UP -> "🔥"
            AchievementType.KEYBOARD_FLYING -> "⌨️"
            AchievementType.HARD_WORKER -> "💪"
            AchievementType.LEGENDARY_1000 -> "📖"
            AchievementType.CODE_MACHINE -> "🤖"
            
            // 按删除
            AchievementType.DELETE_DB -> "🗑️"
            AchievementType.REFACTOR_MASTER -> "♻️"
            AchievementType.DELETE_AND_RUN -> "💣"
            
            // 按时间
            AchievementType.EARLY_BIRD -> "🌞"
            AchievementType.NIGHT_CODER -> "🌙"
            AchievementType.MARATHON_CODER -> "🏃"
            AchievementType.WEEKEND_WARRIOR -> "📅"
            
            // 按连续性
            AchievementType.SIGN_IN_DAY1 -> "🌱"
            AchievementType.CRAZY_7_DAYS -> "🔥"
            AchievementType.IRON_30_DAYS -> "💪"
            AchievementType.DREAM_100_DAYS -> "💯"
            
            // 趣味彩蛋
            AchievementType.BUG_HARVESTER -> "🐛"
            AchievementType.CTRL_S_BELIEVER -> "💾"
            AchievementType.COPY_PASTE_MASTER -> "📋"
            AchievementType.IDEA_RESTARTER -> "🔄"
            AchievementType.CONSOLE_LOG_PRIEST -> "📜"
            
            // 网络热梗成就
            AchievementType.CHICKEN_YOU_ARE_TOO_BEAUTIFUL -> "🏀"
            AchievementType.HAVE_INSIDER_ABORT_DEAL -> "🚨"
            AchievementType.ABSOLUTELY_AMAZING -> "✨"
            AchievementType.NOT_UNUSABLE -> "🤷‍♂️"
            AchievementType.TERRIFYING_WHEN_THINK -> "😱"
            AchievementType.PARALYZED -> "😵"
            AchievementType.CANT_HOLD_IT -> "🤣"
            AchievementType.PURE_GRUDGE_HOLDER -> "😭"
            AchievementType.DEFENSE_BROKEN -> "💔"
            AchievementType.ART_IS_EXPLOSION -> "💥"
            AchievementType.YYDS_FOREVER -> "👑"
            AchievementType.WHAT_HAPPENED -> "📞"
            AchievementType.SOCIAL_DEATH -> "😳"
            AchievementType.LYING_FLAT -> "🛏️"
            AchievementType.INVOLUTION_KING -> "🌀"
        }
    }
} 